package cn.vastbase.jdbc.base;

import org.junit.Test;

import java.sql.*;
import java.time.LocalDate;

public class DateTypeTestCase {
    /* 定义连接对象 */
    static Connection conn = null;
    static String cname = "org.postgresql.Driver";
    static String url = "jdbc:postgresql://172.16.55.175:6666/zxsdb?loggerLevel=OFF";
    static String username = "zxs";
    static String passwd = "Vastdata#0730";
    /* 定义 SQL 语句执行对象 */
    static Statement stmt = null;
    static PreparedStatement pstmt = null;
    /* 定义结果集对象 */
    static ResultSet rs = null;



    @Test
    public void selectByDate() {
        try {
            if (conn() == null)
                return;
            pstmt = conn.prepareStatement("select title from t where log_date between ? and ?");
            pstmt.setDate(1, Date.valueOf(LocalDate.of(2024, 8, 25)));
            pstmt.setDate(2, Date.valueOf(LocalDate.of(2024, 8, 27)));
            rs = pstmt.executeQuery();
            while (rs.next()) {
                System.out.println("prague_sys_log[title=" + rs.getString(1) + "]");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            close();
        }
    }

    @Test
    public void selectTinyint() {
        try {
            if (conn() == null)
                return;
            stmt = conn.createStatement();
            rs = stmt.executeQuery("select a from t_tint1");
            while (rs.next()) {
                Object obj = rs.getObject(1);
                System.out.println("t_tint[a=" + obj.getClass().getSimpleName() + "]");
                System.out.println("t_tint[a=" + obj + "]");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            close();
        }
    }


    public Connection conn() {
        try {
            Class.forName(cname);
            return conn = DriverManager.getConnection(url, username, passwd);
        } catch (ClassNotFoundException | SQLException e) {
            System.out.println("[FAIL] conn database fail." + e.getMessage());
        }
        return null;

    }

    public void close() {
        try {
            /* 关闭结果集对象 */
            if (rs != null)
                rs.close();
        } catch (SQLException e) {
            System.out.println("[FAIL] close resultSet fail." + e.getMessage());
        }
        try {
            /* 关闭语句对象 */
            if (stmt != null)
                stmt.close();
        } catch (SQLException e) {
            System.out.println("[FAIL] close statement fail." + e.getMessage());
        }
        try {
            /* 关闭语句对象 */
            if (pstmt != null)
                pstmt.close();
        } catch (SQLException e) {
            System.out.println("[FAIL] close statement fail." + e.getMessage());
        }
        try {
            /* 关闭连接 */
            conn.close();
        } catch (SQLException e) {
            System.out.println("[FAIL] close connection fail." + e.getMessage());
        }
    }
}
